package com.example.base.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class SystemUtils {

    public static void main(String[] args) {
        getMacAddress();
    }

    public static String getMacAddress(){
        try {
            String os = System.getProperty("os.name").toLowerCase();

            if (("Windows").equals(os)) {
                // Windows系统下获取MAC地址
                Process process = Runtime.getRuntime().exec("ipconfig /all");
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    if (line.contains("Physical Address")) {
                        System.out.println("MAC Address: " + line.split(":")[1].trim());
                        break;
                    }
                }
            } else if (os.contains("mac")) {
                // macOS系统下获取MAC地址
                Process process = Runtime.getRuntime().exec("ifconfig");
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    if (line.contains("ether")) {
                        System.out.println("MAC Address: " + line.split(" ")[1]);
                        break;
                    }
                }
            } else if (os.contains("linux")) {
                // Linux系统下获取MAC地址
                Process process = Runtime.getRuntime().exec("ifconfig");
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    if (line.contains("ether")) {
                        System.out.println("MAC Address: " + line.split(" ")[1]);
                        break;
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }


}
